<template>
  <div class="goods-list">
    <el-card shadow="never">
      <el-form ref="searchForm" :model="form" label-width="140px" label-position="right">
        <el-row>
          <el-col :md="8">
            <el-form-item label="供应商名称">
              <el-input
                v-model="form.providerName"
                clearable
                placeholder="请输入供应商名称"
              />
            </el-form-item>
          </el-col>
          <el-col :md="8">
            <el-form-item label="姓名">
              <el-input
                v-model="form.principal"
                clearable
                placeholder="请输入姓名"
              />
            </el-form-item>
          </el-col>
          <el-col :md="8">
            <el-form-item label="电话">
              <el-input
                v-model="form.principalTel"
                clearable
                placeholder="请输入电话"
              />
            </el-form-item>
          </el-col>
          <el-col :md="8">
            <el-form-item label="审核状态">
              <el-select v-model="form.auditStatus" clearable placeholder="请选择审核状态">
                <el-option v-for="item in auditStatusData" :key="item.value" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="8">
            <el-form-item label="启用状态">
              <el-select v-model="form.status" clearable placeholder="请选择审核状态">
                <el-option v-for="item in statusData" :key="item.value" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="8">
            <el-form-item label="下单日期">
              <el-date-picker
                v-model="times"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
          </el-col>
          <el-col :md="24">
            <el-form-item align="right">
              <el-button type="primary" @click="getAccessAuditedProvider()">查 询</el-button>
              <el-button plain @click="resetForm()">重 置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        :header-cell-style="{background:'#f2f3f5',color:'#606266'}"
        class="table-data"
      >
        <el-table-column prop="id" label="ID" align="center" :show-overflow-tooltip="true" min-width="60" />
        <el-table-column prop="providerName" label="供应商名称" align="center" :show-overflow-tooltip="true" min-width="150" />
        <el-table-column prop="attachUrl" label="营业执照" align="center" :show-overflow-tooltip="true" min-width="150">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.attachUrl"
              :preview-src-list="[scope.row.attachUrl]"
              fit="fit"
            />
          </template>
        </el-table-column>
        <el-table-column prop="address" label="供应商地址" align="center" :show-overflow-tooltip="true" min-width="150" />
        <el-table-column prop="principal" label="负责人" align="center" :show-overflow-tooltip="true" min-width="150" />
        <el-table-column prop="principalTel" label="联系电话" align="center" :show-overflow-tooltip="true" min-width="150" />
        <el-table-column prop="gmtCreate" label="入驻时间" align="center" :show-overflow-tooltip="true" min-width="180">
          <template slot-scope="scope">
            <span> {{ formatTimes(scope.row.gmtCreate,"YYYY/MM/DD hh:mm:ss") }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="启用状态" align="center" :show-overflow-tooltip="true" min-width="150">
          <template slot-scope="scope">
            <div class="status">
              <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                :active-value="1"
                :inactive-value="0"
                @change="changeStatus(scope.row)"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="profitScale" label="分润比例(%)" align="center" :show-overflow-tooltip="true" min-width="220">
          <template slot-scope="scope">
            <span v-if="!scope.row.showProfitScale" class="profit-scale">{{ scope.row.profitScale }} <i class="el-icon-edit icon-edit" @click="changeScale(scope.row,scope.$index)" /></span>
            <div v-if="scope.row.showProfitScale" class="scale-change">
              <el-input v-model="scope.row.profitScale" class="scale-input" size="mini" placeholder="请输入分润比例" />
              <el-button size="mini" type="text" @click="changeProfitScale(scope.row,scope.$index)">确 定</el-button>
              <el-button size="mini" class="cancel-btn" type="text" @click="cancelScale(scope.$index)">取消</el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="120"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="viewSettledIn(scope.row)">查 看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :current-page.sync="page.pageNo"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { getAccessAuditedProvider, providerEnable, providerDisable, providerModifyScale } from '@/api/supplier'
import { auditStatusData, statusData } from './common'
import { formatTimes } from '@/utils'
export default {
  data() {
    return {
      form: {},
      tableData: [],
      formatTimes,
      loading: false,
      auditStatusData,
      statusData,
      times: [],
      page: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  computed: {
  },
  mounted() {
    this.getAccessAuditedProvider()
  },
  methods: {
    // 获取待审核列表
    async getAccessAuditedProvider() {
      try {
        this.loading = true
        const parmas = {
          ...this.form,
          beginDate: this.times && this.times.length > 0 ? this.times[0] : '',
          endDate: this.times && this.times.length > 0 ? this.times[1] : '',
          ...this.page
        }
        const res = await getAccessAuditedProvider(parmas)
        const data = res.objects
        if (data && data.length > 0) {
          data.forEach(item => {
            item.showProfitScale = false
          })
        }

        this.loading = false
        this.tableData = data
        this.page.total = res.total
      } catch (error) {
        this.loading = false
        console.log(error)
      }
    },
    // 改变状态
    async changeStatus(row) {
      if (process.env.VUE_APP_PACKAGE === 'externalize') {
        try {
          await this.$confirm('亲，这是演示版，你没有权限!,想获得所有权限，请联系咨询老师，获取代码', '系统提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
        } catch (error) {
          console.log(error)
        }
        return
      }
      try {
        const formData = new FormData()
        formData.append('accessProviderId', row.id)
        if (row.status == 0) {
          await providerDisable(formData)
        } else {
          await providerEnable(formData)
        }
        this.getAccessAuditedProvider()
      } catch (error) {
        console.log(error)
      }
    },
    // 打开分润比例输入框
    changeScale(row, index) {
      this.tableData[index].showProfitScale = true
    },
    // 关闭分润比例输入框
    cancelScale(index) {
      this.tableData[index].showProfitScale = false
      this.getAccessAuditedProvider()
    },
    // 改变分润比例
    async changeProfitScale(row, index) {
      if (process.env.VUE_APP_PACKAGE === 'externalize') {
        try {
          await this.$confirm('亲，这是演示版，你没有权限!,想获得所有权限，请联系咨询老师，获取代码', '系统提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
        } catch (error) {
          console.log(error)
        }
        return
      }
      try {
        await providerModifyScale({ id: row.id, profitScale: row.profitScale })
        this.tableData[index].showProfitScale = false
        this.$message.success('调整成功')
      } catch (error) {
        this.$message.success(error.message)
        console.log(error)
      }
    },
    // 重置搜索
    resetForm() {
      this.$nextTick(() => {
        this.form = {}
      })
    },
    // 查看
    viewSettledIn(row) {
      this.$router.push({
        path: '/supplier/examine/detail',
        query: {
          id: row.id,
          isExamine: false
        }
      })
    },
    // 切换条
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getAccessAuditedProvider()
    },
    // 切换页
    handleCurrentChange(val) {
      this.page.pageNo = val
      this.getAccessAuditedProvider()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-select{
  width: 100%;
}
.table-data{
  margin: 30px 0px;
  .profit-scale{
    font-weight: 500;
    .icon-edit{
      font-size: 14px;
      font-weight: 500;
      color: #409eff;
      cursor: pointer;
      margin-left: 10px;
    }
  }

  .scale-change{
    display: flex;
    justify-content: space-between;
    .scale-input{
      margin-right: 10px;
      flex: 1;
    }
    .cancel-btn{
      color: #C0C4CC;
    }
  }
}
.el-date-editor{
  width: 100%;
}
.pagination{
  text-align: right;
}
</style>
